/*
 * Created on 2005/04/30
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 */
public class DefaultZoom implements Zoom{
    double[] zoomValues={0.0625,0.125,0.25,0.5,1,2,4,8};;
    int index;
    int preIndex;
    List zoomListener=new ArrayList();
    
    public DefaultZoom(){
        index=4;// 1.0
    }
    public void addZoomChangeListener(ZoomChangeListener listener){
        zoomListener.add(listener);
    }
    
    public void removeZoomChangeListener(ZoomChangeListener listener){
        zoomListener.remove(listener);
    }
    
    public void fireZoomUp(){
        ZoomChangeListener[] listener=getListeners();
        for (int i = 0; i < listener.length; i++) {
            listener[i].zoomUp(this);
        }
    }
    
    public void fireZoomDown(){
        ZoomChangeListener[] listener=getListeners();
        for (int i = 0; i < listener.length; i++) {
            listener[i].zoomDown(this);
        }
    }
    
    public void fireZoomChange(){
        ZoomChangeListener[] listener=getListeners();
        for (int i = 0; i < listener.length; i++) {
            listener[i].zoomChange(this);
        }
    }
    
    public synchronized ZoomChangeListener[] getListeners(){
        return (ZoomChangeListener[])zoomListener.toArray(new ZoomChangeListener[zoomListener.size()]);
    }
    
    boolean wrap;
    public int getZoomIndex() {
        return index;
    }
    private void setIndex(int index) {
        this.index = index;
    }
    public int getPreIndex() {
        return preIndex;
    }
    public void setPreIndex(int preIndex) {
        this.preIndex = preIndex;
    }
    public boolean isWrap() {
        return wrap;
    }
    public void setWrap(boolean wrap) {
        this.wrap = wrap;
    }
    public double getZoom(){
        return zoomValues[index];
    }
    public void zoomUp(){
        if(index+1<zoomValues.length){
            setZoomIndex(index+1);
            fireZoomUp();
        }else{
            if(wrap){
                setZoomIndex(0);
                fireZoomDown();
            }
        }
    }
    
    public void setZoomValue(double zoomValue){
    	for (int i = 0; i < zoomValues.length; i++) {
		if(zoomValues[i]==zoomValue){
			setZoomIndex(i);
			}
		}
    }
    
    public void setZoomIndex(int value){
        preIndex=index;
        index=value;
        fireZoomChange();
    }
    
    
    public void zoomDown(){
        if(index>0){
            setZoomIndex(index-1);
            fireZoomDown();
        }else{
            if(wrap){
                setZoomIndex(index-1);
                fireZoomUp();
            }
        }
    }
    
    public double getZoomValue(int index){
        return zoomValues[index];
    }
    public int countZooms(){
        return zoomValues.length;
    }
    
    public double[] getZooms() {
        return zoomValues;
    }
    public void setZooms(double[] zooms) {
        this.zoomValues = zooms;
    }
}
